/*
 * @Description: 右击菜单
 * @Author: bxsdhx
 * @Date: 2023-09-04 16:52:38
 * @LastEditTime: 2024-07-12 18:05:39
 * Copyright (C) 2023 bxsdhx. All rights reserved.
 */
import type { RightClickMenu } from "@/type";
import { createApp } from "vue";
import view from "./index.vue";
export const viewRightClickMenu = view;
/**
 * @description: 右击菜单
 * @param {MouseEvent} event 鼠标事件
 * @param {RightClickMenu[]} menuList 菜单列表
 * @param {Function} closeCallback 关闭回调
 * @return {void}
 */
let menuComponent: InstanceType<typeof view>;
export default function (
  event: MouseEvent,
  menuList: RightClickMenu[] = [],
  closeCallback?: () => void
): void {
  event.stopPropagation();
  event.preventDefault();
  if (menuComponent) menuComponent.handleClose();
  const container = document.createElement("div");
  const app = createApp(view, {
    close() {
      app.unmount();
      container.remove();
      typeof closeCallback === "function" ? closeCallback() : null;
    },
  });
  menuComponent = app.mount(container) as InstanceType<typeof view>;
  document.body.appendChild(container);
  if (menuComponent) {
    menuComponent.handleOpen(event, menuList);
  }
}
